package com.lanlian.cayman.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanlian.cayman.R;
import com.lanlian.commonlib.bean.RespTips;
import com.lanlian.utillib.DisplayUtil;

import java.util.Timer;
import java.util.TimerTask;

/**
 * <pre>
 *     author : huxiaobo
 *     e-mail : huxiaobo@xlanlian.com
 *     date   : 2018/9/6 19:56
 *     desc   : todo
 *     version: 1.0
 * </pre>
 */
public class ShowAgreementDialog extends Dialog {

    private Context mContext;
    private TextView mTvAgree;
    private WebView mWebView;
    private ImageView mImgClose;
    private OnClickListener mOnClickListener;
    private RespTips mRespTips;

    public ShowAgreementDialog(@NonNull Context context, RespTips respTips, OnClickListener onClickListener) {
        super(context, R.style.customDialog);
        this.mContext = context;
        this.mRespTips = respTips;
        this.mOnClickListener = onClickListener;
    }

    public interface OnClickListener {
        void onClick();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_show_agreement);
        Window window = getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = (int) (DisplayUtil.getScreenWidth(mContext) * 0.8);
        window.setAttributes(lp);
        mTvAgree = findViewById(R.id.tv_agree);
        mWebView = findViewById(R.id.web_view);
        mImgClose = findViewById(R.id.img_close);
        if (mRespTips.getData() != null) {
            mWebView.loadDataWithBaseURL(null, mRespTips.getData().getVcMessageContent(),
                    "text/html", "UTF-8", null);
        }
        TimeCount timeCount = new TimeCount(2000 + 1080, 1000);
        timeCount.startTime();
        mTvAgree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnClickListener.onClick();
                dismiss();
            }
        });
        mImgClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    private class TimeCount extends CountDownTimer {

        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {
            mTvAgree.setText("我已阅读并同意");
            mTvAgree.setEnabled(true);
            cancel();
        }

        @Override
        public void onTick(long millisUntilFinished) {
            if (Math.round((double) (millisUntilFinished - 1000) / 1000) + 1 == 0) {
                return;
            }
            mTvAgree.setText(String.format("我已阅读并同意（%dS）", Math.round((double) (millisUntilFinished - 1000) / 1000) + 1));
        }

        /**
         * 开始计时
         */
        public void startTime() {
            Timer timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    start();
                }
            }, 500);
        }

    }

    @Override
    protected void onStop() {
        super.onStop();
        mWebView = null;
    }
}
